﻿using System.Web.Routing;
using NUnit.Framework;
using WeBlog.Tests.Helpers;

namespace WeBlog.Tests
{
    [TestFixture]
    public class CustomRouteTests
    {
        private RouteData TestRoute( string relativePath )
        {
            var routeConfig = new RouteCollection();
            MvcApplication.RegisterRoutes(routeConfig);
            var context = MvcMockHelpers.FakeHttpContext(relativePath);
            return routeConfig.GetRouteData(context);
        }

        [Test]
        public void Forward_Slash_Goes_To_Post_Index()
        {
            //Arrange

            //Act
            var routeData = TestRoute("~/");

            //Assert
            Assert.AreEqual("Post", routeData.Values["controller"]);
            Assert.AreEqual("Index", routeData.Values["action"]);
        }

        [Test]
        public void Tag_By_Slug_Goes_To_Tag_Details()
        {
            //Arrange

            //Act
            var routeData = TestRoute("~/Tags/FooBar");

            //Assert
            Assert.AreEqual("Tag", routeData.Values["controller"]);
            Assert.AreEqual("Details", routeData.Values["action"]);
        }

        [Test]
        public void Category_By_Slug_Goes_To_Tag_Details()
        {
            //Arrange

            //Act
            var routeData = TestRoute("~/Categories/FooBar");

            //Assert
            Assert.AreEqual("Category", routeData.Values["controller"]);
            Assert.AreEqual("Details", routeData.Values["action"]);
        }

        [Test]
        public void Post_By_Slug_Goes_To_Post_Details()
        {
            //Arrange

            //Act
            var routeData = TestRoute("~/Posts/FooBar");

            //Assert
            Assert.AreEqual("Post", routeData.Values["controller"]);
            Assert.AreEqual("Details", routeData.Values["action"]);
        }

        [Test]
        public void Page_By_Slug_Goes_To_Page_Details()
        {
            //Arrange

            //Act
            var routeData = TestRoute("~/Pages/FooBar");

            //Assert
            Assert.AreEqual("Page", routeData.Values["controller"]);
            Assert.AreEqual("Details", routeData.Values["action"]);
        }

        [Test]
        public void Images_By_Id_Goes_To_Home_GetImage()
        {
            //Arrange

            //Act
            var routeData = TestRoute("~/Images/1");

            //Assert
            Assert.AreEqual("Home", routeData.Values["controller"]);
            Assert.AreEqual("GetImage", routeData.Values["action"]);
        }

        [Test]
        public void Files_By_Id_Goes_To_Home_GetFile()
        {
            //Arrange

            //Act
            var routeData = TestRoute("~/Files/1");

            //Assert
            Assert.AreEqual("Home", routeData.Values["controller"]);
            Assert.AreEqual("GetFile", routeData.Values["action"]);
        }
    }
}
